<template>
  <div class="credit-guide" v-loading="loadingInstance" element-loading-text="数据加载中">
    <iframe style="width: 100%;height: 480px;border: 0" :src="smrzUrl" id="test"></iframe>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import {approveSmrz} from '../../api/tszs/tszs';
import {queryToken} from '../../api/login/login';

export default {
  name: "ztSmrz",
  data(){
    return{
      smrzUrl:'',
      loadingInstance:true
    }
  },
  computed: mapState({
    user(state) {
      return state.user;
    }
  }),
  destroyed() {
    window.addEventListener('message',this.handlerSmrz,false);
  },
  mounted(){
    let _ = this;
    window.removeEventListener('message',this.handlerSmrz,false);
    queryToken().then(res=>{
      if(res.code==200){
        _.loadingInstance = false;
        _.smrzUrl = process.env.ZT_SMRZ+'?token='+res.data;
        window.addEventListener('message',_.handlerSmrz,false);
      }
    }).catch(e=>{
      console.log('系统异常，请联系管理员！'+process.env.LLT_KF_PHONE);
      _.loadingInstance = false;
    })
  },
  methods: {
    handlerSmrz(event) {
      console.log('开始测试1：'+JSON.stringify(event.data));
      if(event.data.status==='ok'){
        this.queryStatusByzt(event.data.nsrsbh,event.data.nsrmc);
      }
     /* this.queryStatusByzt(event.data.nsrsbh,event.data.nsrmc);*/
    },
    /***
     * 轮询
     * 中天实名状态
     * 1税控盘认证成功
     * 0税控盘认证失败
     */
    queryStatusByzt(uscCode,nsrmc){
      let _ = this;
      let loginAccount = this.user.user.loginAccount?this.user.user.loginAccount:'';
      console.log('approveSmrz被调用')
      approveSmrz(uscCode,loginAccount,nsrmc).then(res=>{
        if(res.code==200){
          if(res.data=='1'){//已实名
            _.$emit('zt-smrz');
          }else{
            _.$message({
              message: res.message,
              type: 'error'
            });
          }
        }
      });
    },
    goSelectBank() {
      this.$router.push({
        path: '/productList'
      });
    }
  }
}
</script>
<style lang="scss">
.credit-guide {
  height: 480px;
  .gc-top_title {
    text-align: center;
    font-weight: bold;
    font-size: 18px;
  }

  .gc-top_title:first-child {
    margin-top: 60px;
  }

  .gc-mid_title {
    text-align: center;
    font-weight: bold;
    font-size: 16px;
  }

  .gc-middle_title {
    text-align: center;
    font-size: 14px;

    .com-phone {
      color: #FFA01D;
      padding-left: 20px;
    }
  }

  .gc-bot_title {
    margin-top: 56px;
  }

  .gc-bottom_title {
    font-size: 12px;
    color: #FFA01D;
  }
}
</style>
